<?php

namespace app\index\traits;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\common\model\ConsultingServer;
use app\common\model\Member;
use app\index\service\MessageSendService;

/**
 * 消息发送
 * Trait MessageSendTrait
 * @package app\index\traits
 */
trait MessageSendTrait
{
    /**
     * 律师接单推荐消息 + 畅聊卡片消息（随机匹配）
     * @param Consulting $consulting
     * @param int $consultingServerId 推荐的服务
     * @return void
     */
    private function sendLawyersAndRecommendServerMessage(Consulting $consulting, $consultingServerId)
    {

        $lawyers = Member::where('status', '1')
            ->where('type', '1')
            ->where('checkStatus', '1')
            ->where('muted', '0')
            ->field(['id', 'nickname', 'realname', 'type', 'headImgUrl', 'cropImgUrl'])
            ->append(['showName', 'headImgThumb', 'cropImg'])
            ->order('matchStatus', 'DESC')
            ->limit(3)
            ->select();

        $lawyerName = '李小鹏';
        if (!$lawyers->isEmpty()) {
            $lawyerName = $lawyers[0]->showName;
        }

        $server = null;
        if (is_id($consultingServerId)) {
            $server = ConsultingServer::where('id', $consultingServerId)->find();
        }

        $content = [
            'lawyers' => $lawyers,
            'templates' => [
                [
                    'type' => 'text', 'red' => '102', 'text' => mb_substr($lawyerName . '等', 0, 6),
                ],
                [
                    'type' => 'text', 'red' => '101', 'text' => mt_rand(50, 80),
                ],
                [
                    'type' => 'text', 'red' => '102', 'text' => '位律师愿意接单',
                ],
            ],
        ];

        if ($server) {

            $btnStr = '立即使用';

            // XX律师愿意接单消息
            $content['text'] = '已有金牌律师愿意接单，请点击下方“' . $btnStr . '”，支付成功后将为您提供专业解答服务。';
            $this->getMessageSendService()->sendRobotMessage($consulting, null, 0, ConsultingMessage::LAWYERS, $content);

            // 推荐服务消息
            $content = [
                'id' => $consultingServerId,
                'btnStr' => $btnStr,
                // cancel_card 101请求取消接口 102前端拦截弹窗
                'cancel_card' => '102',
            ];
            $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::SERVER, $content);

        } else {

            // XX律师愿意接单消息
            $content['text'] = '已有金牌律师愿意接单，点击顶部功能立即开始畅聊';
            $this->getMessageSendService()->sendRobotMessage($consulting, null, 0, ConsultingMessage::LAWYERS, $content);
        }
    }

    /**
     * 律师接单推荐消息 + 畅聊卡片消息 （指定律师）
     * @param Consulting $consulting
     * @param $consultingServerId
     * @return void
     */
    private function sendLawyersAndRecommendServerMessage2(Consulting $consulting, $consultingServerId)
    {
        $lawyerId = $consulting->lawyerId;

        $lawyer = Member::where('id', $consulting->lawyerId)
            ->field(['id', 'nickname', 'realname', 'type', 'headImgUrl', 'cropImgUrl'])
            ->append(['showName', 'headImgThumb', 'cropImg'])
            ->find();

        $lawyerName = '';
        if ($lawyer) {
            $lawyerName = $lawyer->showName;
        }

        $server = null;
        if (is_id($consultingServerId)) {
            $server = ConsultingServer::where('id', $consultingServerId)->find();
        }

        if ($server) {

            $btnStr = '立即使用';
            // 给用户发消息
            $content = [
                'text' => '如需现在解答，请点击下方“'.$btnStr.'”，我将为您提供在线沟通服务和专业解决方案。',
                'lawyer' => $lawyer,
                'text_list' => [
                    $lawyerName . '律师正在准备接入中...',
                    $lawyerName . '律师已通过执业认证'
                ]
            ];
            $this->getMessageSendService()->sendMemberMessage($consulting, '1', $lawyerId, ConsultingMessage::TEXT_ROTATIONAL, $content);

            // 推荐服务消息
            $content = [
                'id' => $consultingServerId,
                'btnStr' => $btnStr,
                // cancel_card 101请求取消接口 102前端拦截弹窗
                'cancel_card' => '101',
            ];
            $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::SERVER, $content);

        } else {

            // 给用户发消息
            $content = [
                'text' => '您的问题已收到，正在为您分析案情，请稍等。',
            ];
            $this->getMessageSendService()->sendMemberMessage($consulting, '1', $lawyerId, ConsultingMessage::TEXT, $content);

        }
    }

    /**
     * 律师接单推荐消息 + 畅聊卡片消息（取消指定变随机匹配）
     * @param Consulting $consulting
     * @param int $consultingServerId 推荐的服务
     * @return void
     */
    private function sendLawyersAndRecommendServerMessage3(Consulting $consulting, $consultingServerId, $beforeName)
    {

        $lawyers = Member::where('status', '1')
            ->where('type', '1')
            ->where('checkStatus', '1')
            ->where('muted', '0')
            ->field(['id', 'nickname', 'realname', 'type', 'headImgUrl', 'cropImgUrl'])
            ->append(['showName', 'headImgThumb', 'cropImg'])
            ->order('matchStatus', 'DESC')
            ->limit(3)
            ->select();

        $lawyerName = '李小鹏';
        if (!$lawyers->isEmpty()) {
            $lawyerName = $lawyers[0]->showName;
        }

        $server = null;
        if (is_id($consultingServerId)) {
            $server = ConsultingServer::where('id', $consultingServerId)->find();
        }

        $content = [
            'lawyers' => $lawyers,
            'templates' => [
                [
                    'type' => 'text', 'red' => '102', 'text' => mb_substr($lawyerName . '等', 0, 6),
                ],
                [
                    'type' => 'text', 'red' => '101', 'text' => mt_rand(50, 80),
                ],
                [
                    'type' => 'text', 'red' => '102', 'text' => '位律师愿意接单',
                ],
            ],
        ];

        if ($server) {

            $btnStr = '立即咨询';

            // XX律师愿意接单消息
            // $content['text'] = '已为您取消“'.$beforeName.'律师”的咨询请求，同时为您推荐更多符合条件的律师。点击下方“'.$btnStr.'”将为您分配新的金牌律师。';
            $content['text'] = '已为您取消“' . $beforeName . '律师”的咨询请求，同时为您推荐更多符合条件的律师。';
            $this->getMessageSendService()->sendRobotMessage($consulting, null, 0, ConsultingMessage::LAWYERS, $content);

            // 推荐服务消息
            $content = [
                'id' => $consultingServerId,
                'btnStr' => $btnStr,
                // cancel_card 101请求取消接口 102前端拦截弹窗
                'cancel_card' => '102',
            ];
            $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::SERVER, $content);

        } else {

            // XX律师愿意接单消息
            $content['text'] = '已为您取消“' . $beforeName . '律师”的咨询请求';
            $this->getMessageSendService()->sendRobotMessage($consulting, null, 0, ConsultingMessage::LAWYERS, $content);
        }
    }


    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

}